package hjh.test;

import cn.hjh.pojo.Spare;
import cn.hjh.service.SpareBiz;
import org.junit.Test;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class jdt {


    @Test
    /* 第5章简答题 */
    public void testSpareBysparemodel(){
        List<Spare> result = new SpareBiz().findSpareBysparemodel("CDMA-1");
        for (Spare sp:result) {
            System.out.println("编号：" + sp.getId());
            System.out.println("型号：" + sp.getSparemodel());
            System.out.println("出厂价格：" + sp.getPrice());
        }
    }

    @Test
    //查询出厂价格小于指定价格的备件
    public void testSpareByprice(){
        List<Spare> spareList = new SpareBiz().findSpareByprice();
        for (Spare sp:spareList) {
            System.out.println("编号：" + sp.getId());
            System.out.println("出厂价格：" + sp.getPrice());
            System.out.println("出厂日期：" + sp.getProduction());
        }
    }

    @Test
    //查询出厂日期小于指定日期的备件
    public void testSpareByriqi(){
        List<Spare> spareList = new SpareBiz().findSpareByriqi();
        for (Spare sp:spareList) {
            System.out.println("编号：" + sp.getId());
            System.out.println("型号：" + sp.getSparemodel());
            System.out.println("出厂价格：" + sp.getPrice());
            System.out.println("出厂日期：" + sp.getProduction());
        }
    }

    @Test
    public void SpareBysan(){
        Spare conditions = new Spare();
        conditions.setSparemodel("CDMA-2");
        conditions.setPrice(600.00);
        try {
            conditions.setProduction(new SimpleDateFormat("yyyy-MM-dd").parse("2008-10-24"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Spare> spareList = new SpareBiz().finSpareBysan(conditions);
        for (Spare sp:spareList) {
            System.out.println("编号：" + sp.getId());
            System.out.println("型号：" + sp.getSparemodel());
            System.out.println("出厂价格：" + sp.getPrice());
            System.out.println("出厂日期：" + sp.getProduction());
        }
    }

    @Test
    public void testAdd(){
        Spare spare = new Spare();
        spare.setId(2);
        spare.setSparemodel("CDMA-2");
        spare.setPrice(600.00);
        try {
            spare.setProduction(new SimpleDateFormat("yyyy-MM-dd").parse("2008-10-24"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        new SpareBiz().save(spare);
    }

    @Test
    //@Temporal(TemporalType.DATE)
    public void updateSpare(){
        Spare spare = new Spare();
        spare.setId(new Integer(1));
        spare.setSparemodel("CDMA-1");
        spare.setPrice(new Double(800));
//            spare.setProduction(new Date("2009-9-9"));
//            spare.setProduction(new Timestamp(new Date().getTime()));
        try {
            spare.setProduction(new SimpleDateFormat("yyyy-MM-dd").parse("2009-09-09"));
            new SpareBiz().updateSpare(spare);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testDelete(){
        new SpareBiz().delete(2);
    }
}
